package com.hamizhong.iflowers.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IFestivalDao;
import com.hamizhong.iflowers.db.dao.hibernate.IGoodsDao;
import com.hamizhong.iflowers.db.dao.hibernate.IGoodsFestivalMapDao;
import com.hamizhong.iflowers.db.entity.Festival;
import com.hamizhong.iflowers.db.entity.Goods;
import com.hamizhong.iflowers.db.entity.GoodsFestivalMap;
import com.hamizhong.iflowers.service.IGoodsFestivalMapService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.GoodsFestivalMapVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class GoodsFestivalMapServiceImpl extends MyBaseService implements IGoodsFestivalMapService {
	private IGoodsDao iGoodsDao = null;
	private IFestivalDao iFestivalDao = null;
	private IGoodsFestivalMapDao iGoodsFestivalMapDao = null;
	
	public IGoodsDao getiGoodsDao() {
		return iGoodsDao;
	}
	public void setiGoodsDao(IGoodsDao iGoodsDao) {
		this.iGoodsDao = iGoodsDao;
	}
	public IFestivalDao getiFestivalDao() {
		return iFestivalDao;
	}
	public void setiFestivalDao(IFestivalDao iFestivalDao) {
		this.iFestivalDao = iFestivalDao;
	}
	public IGoodsFestivalMapDao getiGoodsFestivalMapDao() {
		return iGoodsFestivalMapDao;
	}
	public void setiGoodsFestivalMapDao(IGoodsFestivalMapDao iGoodsFestivalMapDao) {
		this.iGoodsFestivalMapDao = iGoodsFestivalMapDao;
	}
	public IOPResultDto<IObjectVO> addGoodsFestivalMaps(List<GoodsFestivalMapVO> goodsFestivalMapVOList) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			for (GoodsFestivalMapVO goodsFestivalMapVO : goodsFestivalMapVOList) {
				GoodsFestivalMap goodsFestivalMap = new GoodsFestivalMap();
				BeanUtils.copyProperties(goodsFestivalMapVO, goodsFestivalMap);
				Goods goods = iGoodsDao.findById(goodsFestivalMapVO.getGoodsVO().getGoodsId());
				goodsFestivalMap.setGoods(goods);
				Festival festival = iFestivalDao.findById(goodsFestivalMapVO.getFestivalVO().getFestivalId());
				goodsFestivalMap.setFestival(festival);
				iGoodsFestivalMapDao.add(goodsFestivalMap);
			}
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【addGoodsFestivalMaps】"+MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
			dto.setSuccess(false);
		}
		return dto;
	}
	
	public IOPResultDto<IObjectVO> deleteGoodsFestivalMaps(List<GoodsFestivalMapVO> goodsFestivalMapVOList) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			for (GoodsFestivalMapVO goodsFestivalMapVO : goodsFestivalMapVOList) {
				GoodsFestivalMap goodsFestivalMap = new GoodsFestivalMap();
				BeanUtils.copyProperties(goodsFestivalMapVO, goodsFestivalMap);
				Goods goods = iGoodsDao.findById(goodsFestivalMapVO.getGoodsVO().getGoodsId());
				goodsFestivalMap.setGoods(goods);
				Festival festival = iFestivalDao.findById(goodsFestivalMapVO.getFestivalVO().getFestivalId());
				goodsFestivalMap.setFestival(festival);
				iGoodsFestivalMapDao.delete(goodsFestivalMap);
			}
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【deleteGoodsFestivalMaps】"+MyErrorMsgConstants.FAILED_TO_DELETE_OPERATE);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_DELETE_OPERATE);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
